﻿
using Microsoft.Extensions.Hosting;

namespace GSOrganization.Application
{
    /// <summary>
    /// 上传图片返回实体类
    /// </summary>
    public class UpLoadFile
    {
        /// <summary>
        /// 图片名称
        /// </summary>
        public string FileName { get; set; }
        /// <summary>
        /// 图片地址
        /// </summary>
        public string FileUrl { get; set; }
    }
    [AllowAnonymous]
    [Route("FileUpLoad")]
    public class FileUpLoad : IDynamicApiController
    {
        private readonly IHostEnvironment _hostEnvironment;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="hostEnvironment"></param>
        public FileUpLoad(IHostEnvironment hostEnvironment)
        {
            _hostEnvironment = hostEnvironment;
        }

        /// <summary>
        /// 监理通知文件上传
        /// </summary>
        /// <param name="files">IFormFile列表</param>
        /// <returns>UpLoadFile列表</returns>
        public List<UpLoadFile> uploadFile(List<IFormFile> files)
        {
            List<UpLoadFile> list = new List<UpLoadFile>();
            if (files.Count > 0)
            {
                var now = DateTime.Now;
                //文件存储路径 (SupervisionNotice)这个名称换成项目所需文件
                var filePath = $"/Uploads/SupervisionNotice/{DateTime.Now:yyyy-MM-dd}/";
                //获取当前web目录
                var webRootPath = _hostEnvironment.ContentRootPath;
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                foreach (var item in files)
                {
                    if (item != null)
                    {
                        #region 文件的条件判断

                        //文件后缀
                        var fileExtension = Path.GetExtension(item.FileName);
                        //判断后缀是否是文件
                        string fileFilt = ".doc|.docx|.pdf";
                        if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1)
                        {
                            //格式不正确时
                            break;
                        }

                        //判断文件大小，超过5M时不让上传    
                        long length = item.Length;
                        if (length > 1024 * 1024 * 5) //5M
                        {
                            break;
                        }

                        #endregion

                        var strDateTime = DateTime.Now.ToString("yyMMddhhmmss"); //取得时间字符串
                        var strRan = new Random().Next(100, 999).ToString(); //生成三位随机数
                        var saveName = strDateTime + strRan + fileExtension;
                        //插入图片数据                 
                        using (FileStream fs = File.Create(filePath + saveName))
                        {
                            item.CopyTo(fs);
                            fs.Flush();
                        }

                        list.Add(new UpLoadFile { FileName = saveName, FileUrl = filePath + saveName });
                    }
                }

            }

            return list;
        }
    }
}